package com.homework.ch04;
import java.util.Scanner;
import java.util.Random;
public class Test08 {

        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            Random random = new Random();

            // 生成长度为20的正整型数组（1-100）
            int[] arr = new int[20];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = random.nextInt(100) + 1;
            }

            System.out.print("生成的数组: ");
            for (int num : arr) {
                System.out.print(num + " ");
            }
            System.out.println();

            while (true) {
                System.out.print("请输入要查找的数字(-1退出): ");
                int input = scanner.nextInt();

                if (input == -1) {
                    System.out.println("程序已退出");
                    break;
                }

                boolean found = false;
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i] == input) {
                        System.out.println("数字 " + input + " 在位置: " + i);
                        found = true;
                    }
                }

                if (!found) {
                    System.out.println("数字 " + input + " 不存在");
                }
            }

            scanner.close();
        }

}
